﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TCG.Engine.Effects;
using System.Reflection;

namespace TCG.Engine.Libraries
{
    public class EffectLibrary : IEffectLibrary
    {
        Dictionary<string, Func<IEffect>> effects = new Dictionary<string, Func<IEffect>>();

        public Dictionary<string, Func<IEffect>> Effects
        {
            get { return effects; }
        }
        
        public void LoadAll(Assembly a)
        {
            var effectTypes = a.GetTypes().Where(t => t.GetCustomAttributes(typeof(EffectAttribute), false).Any());
            foreach (var et in effectTypes)
            {
                var attr = (EffectAttribute)et.GetCustomAttributes(typeof(EffectAttribute), false).Single();
                var effect = et;
                Add(attr.Id, () => (IEffect)Activator.CreateInstance(effect));
            }
        }
        
        public void Add(string id, Func<IEffect> creator)
        {
            effects[id] = creator;
        }

        public IEffect Get(string id)
        {
            return effects[id]();
        }
    }
}
